<?php
class CommonModel extends RelationModel {
    
    protected $_auto                = array ( 
                                             array('user_id', 'get_login_user_id', 3, 'function'),
                                             array('create_time', 'get_date', 1, 'function'),
                                             array('update_time', 'get_date', 3, 'function'),
                                      );
        
    protected $_link                = null;
    protected $_arr_status          = array(
                                            'default' => array(
                                                               '1'  => '有效',
                                                               '0'  => '<span class="red">无效</span>',
                                                               '-1' => '<span class="red">删除</span>',
                                                         ),
                                            'bool'    => array(
                                                               '1'  => '是',
                                                               '0'  => '<span class="red">否</span>',
                                                         ),
                                            'online'  => array(
                                                               '1'  => '上线',
                                                               '0'  => '<span class="red">下线</span>',
                                                         ),  
                                            'onwork'  => array(
                                                               '1'  => '在职',
                                                               '0'  => '<span class="red">离职</span>',
                                                         ),   
                                      );  
               
    protected $_str_order_by        = 'create_time DESC';// Order by default
    
    protected $_str_field_map       = 'id, name';// For method get_field_map
    
    protected static $_bln_trans    = false;

    /**
     * Get info by pk id
     * 
     * @param  $strPkId
     * @param  $strField
     * @param  $strSep
     * @return mix
     * @author andy
     */
    public function get_info_by_pk( $strPkId, $strField, $strSep = ', ' ) {

        if ( empty($strPkId) || empty($strField) ) {
            return false;
        }
        
        $arr_info = $this->where($this->getPk() . ' IN (' . $strPkId . ')')->getField($this->getPk() . ',' . $strField);

        return $strSep ? implode($strSep, $arr_info) : $arr_info;
    }
    
    /**
     * Get pk by name
     * 
     * @param  $mixFieldVal
     * @param  $strFieldName
     * @param  $strSep
     * @return mix
     * @author andy
     */
    public function get_pk_by_name( $mixFieldVal, $strFieldName = 'name', $strSep = ',' ) {

        if ( empty($mixFieldVal) || empty($strFieldName) ) {
            return false;
        }
        
        $arr_where[$strFieldName] = array('LIKE', '%' . $mixFieldVal . '%');
        $arr_info                 = $this->where($arr_where)->getField($this->getPk(), true);

        return $strSep ? implode($strSep, $arr_info) : $arr_info;
    }
    
    /**
     * Get total
     * 
     * @param  $arrWhere
     * @return int
     * @author andy
     */
    public function get_total( $arrWhere = array() ) {

        return $this->where($arrWhere)->count();
    }
    
    /**
     * Get list info
     * 
     * @param  $intPage
     * @param  $intPerPage
     * @param  $arrWhere
     * @param  $strOrderBy
     * @return array
     * @author andy
     */
    public function get_list( $intPage = 1, $intPerPage = 10, $arrWhere = array(), $strOrderBy = '' ) {
        
        $str_order_by = $this->_get_order_by($strOrderBy);
        $bln_relation = $this->_is_relation();
        
        return $this->where($arrWhere)->order($str_order_by)->page($intPage, $intPerPage)->relation($bln_relation)->select();
    }
    
    /**
     * Get all info
     * 
     * @param  $arrParams
     * @param  $strOrderBy
     * @return array
     * @author andy
     */
    public function get_all( $arrWhere = array('status'=>1), $strOrderBy = '' ) {
        
        $str_order_by = $this->_get_order_by($strOrderBy);
        $bln_relation = $this->_is_relation();

        return $this->where($arrWhere)->order($str_order_by)->relation($bln_relation)->select();
    }
    
    /**
     * Get field map
     * 
     * @param  $strField
     * @param  $arrParams
     * @param  $strOrderBy
     * @return array
     * @author andy
     */
    public function get_field_map( $strFieldMap = '', $arrWhere = array('status'=>1), $strOrderBy = '' ) {
        
        $str_field_map = $this->_get_field_map($strFieldMap);
        $str_order_by  = $this->_get_order_by($strOrderBy);
        
        return $this->where($arrWhere)->order($str_order_by)->getField($str_field_map, true);
    }
    
    /**
     * Get status
     * 
     * $_arr_status
     * 
     * @param  $strStatusType
     * @return array
     * @author andy
     */
    public function get_status( $strStatusType = 'default' ) {

        return $strStatusType == 'all' ? $this->_arr_status : $this->_arr_status[$strStatusType];
    }

    /**
     * Get field info
     * get variable array, the definition of class, eg, $_arr_type
     * 
     * @param  $strFieldName
     * @param  $strFieldInfoKey
     * @return array
     * @author andy
     */
    public function get_field_info( $strFieldName, $strFieldInfoKey = '' ) {

        $str_array_key  = '_arr_' . $strFieldName;
        $arr_field_info = $this->$str_array_key;

        return $strFieldInfoKey ? $arr_field_info[$strFieldInfoKey] : $arr_field_info;
    }
    
    /**
     * Start transaction
     * 
     * @param  void
     * @return void
     * @author andy
     */
    public function start_trans() {
        
        if ( self::$_bln_trans ) {
            return ;
        }
        
        self::$_bln_trans = true;
        
        return $this->startTrans();
    }
    
    /**
     * Rollback transaction
     * 
     * @param  void
     * @return void
     * @author andy
     */
    public function rollback_trans() {
        
        self::$_bln_trans = false;
        
        return $this->rollback();
    }
    
    /**
     * Commit transaction
     * 
     * @param  void
     * @return void
     * @author andy
     */
    public function commit_trans() {
        
        self::$_bln_trans = false;
        
        return $this->commit();
    }
    
    /**
     * Get data
     * the processed data by thinkphp
     * 
     * @param  void
     * @return array
     * @author andy
     */
    public function get_data() {
        
        return $this->data;
    }
    
    /**
     * Get order by
     * 
     * @param  $strOrderBy
     * @return string
     * @author andy
     */
    private function _get_order_by( $strOrderBy = '' ) {
        
        return ! empty($strOrderBy) ? $strOrderBy : $this->_str_order_by;
    }
    
    /**
     * Get field map
     * 
     * @param  $strFieldMap
     * @return string
     * @author andy
     */
    private function _get_field_map( $strFieldMap = '' ) {
        
        return ! empty($strFieldMap) ? $strFieldMap : $this->_str_field_map;
    }
    
    /**
     * Whether relation
     * 
     * @param  void
     * @return bool
     * @author andy
     */
    private function _is_relation() {
        
        return ! empty($this->_link);
    }
}